package cn.lijiajia3515.cairo.system.modules.imgproxy;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/imgproxy")
public class ImgproxyApi {
	private final ImgproxyService imgProxyService;

	public ImgproxyApi(ImgproxyService imgProxyService) {
		this.imgProxyService = imgProxyService;
	}

	/**
	 * 获取 资源 url
	 *
	 * @param params 获取参数
	 * @return 已签名的url
	 */
	@PostMapping("/get_objects_url")
	public List<String> getUrl(@RequestBody List<GetImgUrlParam> params) {
		return params.parallelStream()
			.map(x -> {
				try {
					return imgProxyService.getSingedUrl(x.getSourceUrl(), x.getParams());
				} catch (Exception e) {
					return "unknown";
				}
			})
			.collect(Collectors.toList());
	}
}
